package com.admin;


import com.Constants;
import com.admin.MenuAction;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;






public class MenuAction extends MenuService {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 845990288745458548L;
	private List<Menu> menuTree;
	private List<Menu> menus;
	private Menu menu;
	private int menuid;
	private String menuids;
	private String menuname;
	private String age;
	private int page = 1;
	private int limit = 2;
	private static String table="\"Menu\"";
	private String Xgzd;
	private String Value;
	private int Userid;
	
	public String execute() {
		
			return "success";
	}
	//新增-显示菜单表格
		public String dotoshowtabe() {
			menus = selectMenuss();
			ajaxJsontable(menus); 
			return null;
		}

		
		@SuppressWarnings({ "rawtypes", "unchecked" })
		public void getbutton() {
			User loginuser = (User)session.get(Constants.USER);	
			Map map = new HashMap();
			map.put("table", "\"User_Menu_Limit\"");
			map.put("where", "\"Userid\"="+loginuser.getUserid()+" and \"Menuid\"="+menuid);
			map.put("nr", "*");
			map.put("px", "\"Userid\"");
			List<LinkedHashMap<String, String>>	list = selecttable(map);
			ajaxJson(list);
				
		}
		
		@SuppressWarnings({ "rawtypes", "unchecked" })
		public String getqx() {
			User loginuser = (User)session.get(Constants.USER);	
			if(loginuser!=null) {
				Map map = new HashMap();
				map.put("table", "\"User_Menu_Limit\"");
				map.put("where", "\"Userid\"="+loginuser.getUserid()+" and \"Menuid\"="+menuid);
				map.put("nr", "*");
				map.put("px", "\"Userid\"");
				List<LinkedHashMap<String, String>>	list = selecttable(map);
				
				for(int a=0;a<list.size();a++) {
					HashMap<String,String> HashMap =new HashMap<String, String>();;
					HashMap=list.get(a);
					JSONArray jArray = new JSONArray();
					 jArray.put(HashMap);
					 JSONObject jo;
					 
					try {
						jo = jArray.getJSONObject(0);
						if(a==0) {
							 message=jo.getInt("Limitlevel")+"";
						 }else {
							 message=message+","+jo.getInt("Limitlevel")+"";
						 }
						 } catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				  
				 }
				
				return "success";
			}else {
				return "error";
				
			}
			
				
			
		}
		

		@SuppressWarnings({ "rawtypes", "unchecked" })
		public void savemenu() {
			if(menu==null) {
				message="数据丢失！";
				savelog(message);
				ajaxJson(message);
			}else {
				
				
					Map map = new HashMap();
					map.put("Menuname", menu.getMenuname());
					map.put("nr", "*");
					map.put("table", table);
					map.put("Status", "0");
					map.put("px", "\"Menuid\" desc");
					List<LinkedHashMap<String, String>>	list = getlist(map);
					if(list.size()>0) {
						message="1";
						savelog("菜单名称重复");
						ajaxJson(message);
						
						return;
				     }
				
				
				
				Map params = new HashMap();
				params.put("table", "\"Menu\"");
				
				User loginuser = (User)session.get(Constants.USER);
				String zdm="\"Menuname\",\"Menulevel\",\"Pmenuid\",\"Menuurl\",\"Paixu\",\"Xguserid\",\"Createuserid\"";
				params.put("zdm", zdm);
				String nr="'"+menu.getMenuname()+"','"+menu.getMenulevel()+"','"+menu.getPmenuid()+"','"+menu.getMenuurl()+"','"+menu.getPaixu()+"','"+loginuser.getUserid()+"','"+loginuser.getUserid()+"'";
				params.put("nr", nr);
			    inserttable(params);
			    ajaxJson("1");
			    savelog("操作成功");
			}
			
		}
		
		
		
		@SuppressWarnings({ "rawtypes", "unchecked" })
		public void delmenu() {
			if(menuid==0) {
				message="参数丢失";
				ajaxJson(message);
				 savelog(message);
				return;
			}
			
			Map map = new HashMap();
			map.put("Menuid", menuid);
			map.put("nr", "*");
			map.put("table", "\"Role_Menu\"");
			map.put("px", "\"Menuid\" desc");
			List<LinkedHashMap<String, String>>	list = getlist(map);
			if(list.size()>0) {
				
				ajaxJson("数据有关联");
				savelog(message+"操作失败");
				return;
		     }else {
		    	 
		    	 Map params = new HashMap();
					params.put("table", "\"Menu\"");
					String where="\"Menuid\"="+menuid;
					params.put("where", where);
					deletetable(params);
					ajaxJson("1");
					savelog("操作成功");
		    	}
			
			
		}
		
		@SuppressWarnings({ "rawtypes", "unchecked" })
		public void xgmenu() {
			if(menuid==0) {
				message="-1";
				ajaxJson(message);
				savelog("参数丢失，操作失败");
				return;
			}
			
			if(Xgzd.equals("Menuname")) {
				Map map = new HashMap();
				map.put("Menuname", Value);
				map.put("nr", "*");
				map.put("table", table);
				map.put("Status", "0");
				map.put("px", "\"Menuid\" desc");
				List<LinkedHashMap<String, String>>	list = getlist(map);
				if(list.size()>0) {
					message="1";
					ajaxJson(message);
					savelog("菜单重复操作失败");
					return;
				}
			}
			User loginuser = (User)session.get(Constants.USER);
			
			Map params = new HashMap();
			params.put("Keyidvalue", menuid);
			params.put("Keyid", "Menuid");
			params.put("Xgzd",Xgzd);
			params.put("Value",Value);
			params.put("table", table);
			int result=updatetablebykey(params);
			String msg="";
				if(result==0){//修改失败   数据库保存失败
					message="2";
					msg="数据库保存异常";
					
				}else{//修改成功
					params.put("Xgzd","Xguserid");
					params.put("Value",loginuser.getUserid());
					updatetablebykey(params);
					msg="操作成功";
				}
			ajaxJson(message);
			savelog(msg);
		}
		
		
		
		@SuppressWarnings({ "rawtypes", "unchecked" })
		public void showusermenu() {
			Map map=new HashMap();
			map.put("table", "\"User_Role\" a");
			map.put("jointable", "join \"Role_Menu\" b on a.\"Roleid\"=b.\"Roleid\" join \"Menu\" c on c.\"Menuid\"=b.\"Menuid\" join \"Menu\" d on c.\"Pmenuid\"=d.\"Menuid\"");
			map.put("where", "\"Userid\"="+Userid);
			map.put("nr", "distinct c.*,d.\"Menuname\" as \"sjmenu\",\"Userid\",d.\"Menuid\" as \"sjid\"");
			map.put("pxfalse", "1");
			List<LinkedHashMap<String, String>>	list =selecttable(map);
			ajaxJson(list);
			
			
		}
		
		
		@SuppressWarnings({ "rawtypes", "unchecked" })
		public String dotoshowmenutabe() {
			Map params = new HashMap();
			String like="1=1";
			if(menuname!=null) {
				if(!menuname.equals("")) {
					
					like+=" and \"Menuname\" like '%"+menuname+"%'";
				}
				
			}
			
			params.put("Status", "0");
			params.put("like", like);
			params.put("nr", "*");
			params.put("table", table);
			params.put("px", "\"Menuid\" desc");
			List<LinkedHashMap<String, String>>	list = getlist(params);
			if(list.size()>0) {
				
				String strs = gson.toJson(list.get(0));
				JsonParser parser = new JsonParser();
    			JsonElement jsonEl = parser.parse(strs);

    			JsonObject jsonObj = null;
    			jsonObj = jsonEl.getAsJsonObject();//转换成Json对象
    			String number=jsonObj.get("Count").getAsString();//status节点
				ajaxJsontablefy(number,list);	
					
			}else {
				ajaxJsontablefy("0",null);	
					
			}
				return null;
			}
		
		
		
		
		
		public List<Menu> getMenuTree() {
			return menuTree;
		}


		public void setMenuTree(List<Menu> menuTree) {
			this.menuTree = menuTree;
		}


		public List<Menu> getMenus() {
			return menus;
		}


		public void setMenus(List<Menu> menus) {
			this.menus = menus;
		}


		public Menu getMenu() {
			return menu;
		}


		public void setMenu(Menu menu) {
			this.menu = menu;
		}


		public int getMenuid() {
			return menuid;
		}


		public void setMenuid(int menuid) {
			this.menuid = menuid;
		}


		public String getMenuids() {
			return menuids;
		}


		public void setMenuids(String menuids) {
			this.menuids = menuids;
		}


		public String getMenuname() {
			return menuname;
		}


		public void setMenuname(String menuname) {
			this.menuname = menuname;
		}


		public int getPage() {
			return page;
		}


		public void setPage(int page) {
			this.page = page;
		}


		public int getLimit() {
			return limit;
		}


		public void setLimit(int limit) {
			this.limit = limit;
		}
		public String getAge() {
			return age;
		}
		public void setAge(String age) {
			this.age = age;
		}
		public String getXgzd() {
			return Xgzd;
		}
		public void setXgzd(String xgzd) {
			Xgzd = xgzd;
		}
		public String getValue() {
			return Value;
		}
		public void setValue(String value) {
			Value = value;
		}

		
		public int getUserid() {
			return Userid;
		}
		public void setUserid(int userid) {
			Userid = userid;
		}

	
}